
-- draft - not working - needs a lot of work here 

module CreditMarket where

import Types

type CMElem = (Date, -- term
               Cash); -- total



-- structure is a collection of terms (i.e. maturities)
--
-- when there's not enough debt at a particular maturity, we auction some
-- more.
--
-- Note that sometimes longer term offerings will become shorter term, and
-- this will cause less short term and more long term stuff to be issued
-- to keep the term strcture as similar as possible to the ideal structure.
-- 

type CMID = Int

data CreditMarket = 
    CM { id::CMID,
         onIssue::[CMElem],
         idealStructure::[CMElem], 
         defaultRate::Double }

data CreditMarketFactory =
    CMF { idealStructure::[CMElem],
          defaultRate::Double }

{-
Semantice of onIssue and idealStructure are different.  onIssue is the total
debt of that particular maturity on issue.  

e.g:

(0.25,20),(2,30)

means 

20 with a maturtiy of 0.25
30 with a maturity of 2

idealStructure on the other hand is like this:

(0.5,100),(1,50),(2,75)

100 units of debt under 0.5 years maturity
50 units of debt between 0.5 and 1
75 between 1 and 2

When there is insufficient debt of a particular maturity, new debt at the max
maturity is issued.
-}



empty (CMF f) = 
    CM

{-
finds the quantity of credit on issue at particuler maturities

-}


termStructure :: CreditMarket -> [CMElem]
termStructure c = ts1 (

stepCM :: CreditMarket -> CreditMarket
stepCM c = c { onIssue = stepM (onissue c) }
    where stepM [] = []
          stepM (e:es) = (step1 e):(stepM es)
          step1 (d,a) = (decDate d,a)

{-

find the amount of new debt to auction

-}

toAuction :: CreditMarket -> [CMElem]
toAuction c = cElemMinus (idealStructure c) (termStructure c)

cElemMinus [] [] = []
cElemMinus ((d,a1):r1) ((d,a2):r2) = (d,min (a1-a2) 0):(cElemMinus r1 r2)
cElemMinus _ _ = undefined 
